<?php

/**
 * cliente actions.
 *
 * @package    cronos-doctrine
 * @subpackage cliente
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class clienteActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->usuarios = Doctrine_Core::getTable('usuario')
      ->createQuery('a')
      ->execute();
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->usuario = Doctrine_Core::getTable('usuario')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->usuario);
  }

  public function executeNew(sfWebRequest $request)
  {
      $this->form = new usuarioForm();
      
      
      $this->form->setWidget('agente_id', new sfWidgetFormInputHidden(array(),array('value' => $this->getUser()->getAttribute('agente_user'))));
      $this->form->setWidget('status', new sfWidgetFormInputHidden(array(),array('value' => 1)));
      $this->permisos = $this->getUser()->getGuardUser()->getPermissions();
      
      
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));  
    $this->form = new usuarioForm();
    $this->form->setWidget('agente_id', new sfWidgetFormInputHidden(array(),array('value' => $this->getUser()->getAttribute('agente_user'))));
    $this->form->setWidget('status', new sfWidgetFormInputHidden(array(),array('value' => 1)));
    $this->processForm($request, $this->form);
    $this->getUser()->setFlash('error', 'No se pudo Crear el item, existen errores en el formulario.\n Por favor revisar campos resaltado'); 
    $this->setTemplate('new');
  }

  
  
    
  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($usuario = Doctrine_Core::getTable('usuario')->find(array($request->getParameter('id'))), sprintf('Object usuario does not exist (%s).', $request->getParameter('id')));
    $this->form = new usuarioForm($usuario);
    $this->form->setWidget('agente_id', new sfWidgetFormInputHidden(array(),array('value' => $this->getUser()->getAttribute('agente_user'))));
    $this->form->setWidget('status', new sfWidgetFormInputHidden(array(),array('value' => 1)));
    
    
    $this->permisos = $this->getUser()->getGuardUser()->getPermissions();
    //Captura de donde es llamado la edicion
    $this->urlAnt=$request->getParameter('urlAnt');
    
 
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($usuario = Doctrine_Core::getTable('usuario')->find(array($request->getParameter('id'))), sprintf('Object usuario does not exist (%s).', $request->getParameter('id')));
    $this->form = new usuarioForm($usuario);
    $this->form->setWidget('agente_id', new sfWidgetFormInputHidden(array(),array('value' => $this->getUser()->getAttribute('agente_user'))));
    $this->form->setWidget('status', new sfWidgetFormInputHidden(array(),array('value' => 1)));
    $this->processForm($request, $this->form);
    $this->getUser()->setFlash('error', 'No se Actualizar el item, existen errores en el formulario.\n Por favor revisar campos resaltado'); 
    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
     try {
        $request->checkCSRFProtection();
        $this->forward404Unless($usuario = Doctrine_Core::getTable('usuario')->find(array($request->getParameter('id'))), sprintf('Object usuario does not exist (%s).', $request->getParameter('id')));
        $usuario->delete();
        $this->getUser()->setFlash('notice', 'El Item fué removido exitosamente.'); 
        $this->redirect('dashboard/index');
     } catch (Doctrine_Exception $e) {
        $this->getUser()->setFlash('error', 'No se puedo eliminar el item, existen referencias a esta entrada.'); 
        $this->redirect('dashboard/index');
     }    
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {

    
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
        $notice = $form->getObject()->isNew() ? 'El item se creó correctamente.' : 'El item se actualizó correctamente.';  
        
        try {
            $usuario = $form->save();
                   
            
        } catch (Doctrine_Validator_Exception $e) {

        $errorStack = $form->getObject()->getErrorStack();
        $message = get_class($form->getObject()) . ' has ' . count($errorStack) . " field" . (count($errorStack) > 1 ?  's' : null) . " with validation errors: ";
        foreach ($errorStack as $field => $errors) {
            $message .= "$field (" . implode(", ", $errors) . "), ";
        }
        $message = trim($message, ', ');

        $this->getUser()->setFlash('error', $message);
        return sfView::SUCCESS;
      }
           
      $this->getUser()->setFlash('notice', $notice);
      $this->redirect('cliente/edit?id='.$usuario->getId());
    }
  }
  
  public function executeAddEmaForm(sfWebRequest $request)
  {
      $this->forward404unless($request->isXmlHttpRequest());
      $number = intval($request->getParameter("num"));

      $deta = $usuario = Doctrine_Core::getTable('Usuario')->find($request->getParameter('id'));
      if(!is_null($deta)){
            $form = new UsuarioForm($usuario);;
      }else{
            $form = new UsuarioForm();
      }
      
      
      $form->addEmail($number);
      
      
      return $this->renderPartial('addEma',array('form' => $form, 'num' => $number));
  }
  
  public function executeDelEmaForm(sfWebRequest $request)
  {
    $this->forward404Unless($email = Doctrine_Core::getTable('Email')->find(array($request->getParameter('id'))), sprintf('Object galeria does not exist (%s).', $request->getParameter('id')));
    
    try {
           $email->delete();
           $this->getUser()->setFlash('notice', 'El Item fué removido exitosamente.'); 
          
      } catch (Doctrine_Exception $e) {
          $this->getUser()->setFlash('error', 'No se puedo eliminar el item, existen referencias a esta entrada.'); 
          
      }
  }
  

  
  public function executeAddTelForm(sfWebRequest $request)
  {
      $this->forward404unless($request->isXmlHttpRequest());
      $number = intval($request->getParameter("num"));
      
      $deta = $usuario = Doctrine_Core::getTable('Usuario')->find($request->getParameter('id'));
      if(!is_null($deta)){
            
            $form = new UsuarioForm($usuario);
      }else{
            $form = new UsuarioForm();
      }
      
      
      $form->addTelefono($number);

      return $this->renderPartial('addTel',array('form' => $form, 'num' => $number));
  }
  
  public function executeDelTelForm(sfWebRequest $request)
  {
    $this->forward404Unless($tele = Doctrine_Core::getTable('Telefono')->find(array($request->getParameter('id'))), sprintf('Object galeria does not exist (%s).', $request->getParameter('id')));
    
    try {
           $tele->delete();
           $this->getUser()->setFlash('notice', 'El Item fué removido exitosamente.'); 
          
      } catch (Doctrine_Exception $e) {
          $this->getUser()->setFlash('error', 'No se puedo eliminar el item, existen referencias a esta entrada.'); 
          
      }
  }
  
  
  public function executeAddDirForm(sfWebRequest $request)
  {
      $this->forward404unless($request->isXmlHttpRequest());
      $number = intval($request->getParameter("num"));

      $deta = $usuario = Doctrine_Core::getTable('Usuario')->find($request->getParameter('id'));
      if(!is_null($deta)){
            $form = new UsuarioForm($usuario);;
      }else{
            $form = new UsuarioForm();
      }
      
      $form->addDireccion($number);
      return $this->renderPartial('addDir',array('form' => $form, 'num' => $number));
  }
  
  public function executeDelDirForm(sfWebRequest $request)
  {
    $this->forward404Unless($dire = Doctrine_Core::getTable('Direccion')->find(array($request->getParameter('id'))), sprintf('Object galeria does not exist (%s).', $request->getParameter('id')));
    
    try {
           $dire->delete();
           $this->getUser()->setFlash('notice', 'El Item fué removido exitosamente.'); 
          
      } catch (Doctrine_Exception $e) {
          $this->getUser()->setFlash('error', 'No se puedo eliminar el item, existen referencias a esta entrada.'); 
          
      }
  }
  
  public function executeAddMasForm(sfWebRequest $request)
  {
      $this->forward404unless($request->isXmlHttpRequest());
      $number = intval($request->getParameter("num"));

      $deta = $usuario = Doctrine_Core::getTable('Usuario')->find($request->getParameter('id'));
      if(!is_null($deta)){
            $form = new UsuarioForm($usuario);;
      }else{
            $form = new UsuarioForm();
      }
      
      
      $form->addMascota($number);
      return $this->renderPartial('addMas',array('form' => $form, 'num' => $number));
  }
  
  public function executeDelMasForm(sfWebRequest $request)
  {
    $this->forward404Unless($mas = Doctrine_Core::getTable('Mascota')->find(array($request->getParameter('id'))), sprintf('Object galeria does not exist (%s).', $request->getParameter('id')));
    
    try {
           $mas->delete();
           $this->getUser()->setFlash('notice', 'El Item fué removido exitosamente.'); 
          
      } catch (Doctrine_Exception $e) {
          $this->getUser()->setFlash('error', 'No se puedo eliminar el item, existen referencias a esta entrada.'); 
          
      }
  }
  
  public function executeAddParForm(sfWebRequest $request)
  {
      $this->forward404unless($request->isXmlHttpRequest());
      $number = intval($request->getParameter("num"));

      $deta = $usuario = Doctrine_Core::getTable('Usuario')->find($request->getParameter('id'));
      if(!is_null($deta)){
            $form = new UsuarioForm($usuario);;
      }else{
            $form = new UsuarioForm();
      }
      
      $form->addParentesco($number);
      return $this->renderPartial('addPar',array('form' => $form, 'num' => $number));
  }
  
  public function executeDelParForm(sfWebRequest $request)
  {
    $this->forward404Unless($pare = Doctrine_Core::getTable('Parentesco')->find(array($request->getParameter('id'))), sprintf('Object galeria does not exist (%s).', $request->getParameter('id')));
    
    try {
           $pare->delete();
           $this->getUser()->setFlash('notice', 'El Item fué removido exitosamente.'); 
          
      } catch (Doctrine_Exception $e) {
          $this->getUser()->setFlash('error', 'No se puedo eliminar el item, existen referencias a esta entrada.'); 
          
      }
  }
  
  
   public function executeAddPerForm(sfWebRequest $request)
  {
      $this->forward404unless($request->isXmlHttpRequest());
      $number = intval($request->getParameter("num"));

      $deta = $usuario = Doctrine_Core::getTable('Usuario')->find($request->getParameter('id'));
      if(!is_null($deta)){
            $form = new UsuarioForm($usuario);
      }else{
            $form = new UsuarioForm();
      }
      
      $form->addPeriodicidad($number);
      return $this->renderPartial('addPer',array('form' => $form, 'num' => $number));
  }
  
    public function executeDelPerForm(sfWebRequest $request)
  {
    $this->forward404Unless($pare = Doctrine_Core::getTable('LLamadaServicio')->find(array($request->getParameter('id'))), sprintf('Object galeria does not exist (%s).', $request->getParameter('id')));
    
    try {
           $pare->delete();
           $this->getUser()->setFlash('notice', 'El Item fué removido exitosamente.'); 
          
      } catch (Doctrine_Exception $e) {
          $this->getUser()->setFlash('error', 'No se puedo eliminar el item, existen referencias a esta entrada.'); 
          
      }
  }
  

  public function executeSearchColonia(sfWebRequest $request)
  {
   
      
      $municipio_id = $request->getParameter('municipio_id');
      $nombre       = $request->getParameter('nombre');
      
      $colonias = new Parroquia();
      $this->colonias = $colonias->getParroquiaSearch($municipio_id,$nombre);
      
      
  }
    public function executeSearchColoniaSelect(sfWebRequest $request)
  {
   
      
      $municipio_id = $request->getParameter('municipio_id');
      $this->parroquia_id = $request->getParameter('parroquia_id');
      
      $colonias = new Parroquia();
      $this->colonias = $colonias->getParroquia($municipio_id);
      
      
  }

   public function executeSearchColoniaFiltros(sfWebRequest $request)
  {
   
      
      $municipio_id = $request->getParameter('municipio_id');
      $nombre       = $request->getParameter('nombre');
      
      $colonias = new Parroquia();
      $this->colonias = $colonias->getParroquiaSearch($municipio_id,$nombre);
      
      
  }
    public function executeSearchColoniaSelectFiltros(sfWebRequest $request)
  {
   
      
      $municipio_id = $request->getParameter('municipio_id');
      $this->parroquia_id = $request->getParameter('parroquia_id');
      
      $colonias = new Parroquia();
      $this->colonias = $colonias->getParroquia($municipio_id);
      
      
  }
  
  
}
